#!/bin/bash


### init config
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
source /etc/profile

#!/bin/bash
PRE_DIR=`dirname $0`
PROJECT_DIR=/data/web/hf_app/build.wolfapi
PROJECT_NAME=wolf-api
DOC_ROOT=$PROJECT_DIR/ROOT
WAR_FILE=$PROJECT_DIR/$PROJECT_NAME/target/wolf-api.war
TOMCAT=/usr/local/app/tomcat
DATE=`date "+%Y%m%d%H%M%S"`
STR_DATE=`date "+%s"`
backup="backup"

#backup
tar -zcvf ROOT."$DATE".tar.gz $PROJECT_DIR/ROOT
mv ROOT.$DATE.tar.gz $backup/
#deploy
echo
echo Deploy:
echo

cd $PROJECT_DIR/$PROJECT_NAME
 git checkout master
git pull
if [ ! -z $1 ]
then
    git branch -D test
    #git路径
    git fetch  xxx.com/xxx  refs/changes/$1 && git checkout FETCH_HEAD
    git checkout -b test
else
    git  checkout qa
    git pull
    git branch -D test
    git checkout -b test
fi
mvn clean package -DskipTests -Ptest -U -o  
rm -rf $PROJECT_DIR/ROOT
mkdir -p  $PROJECT_DIR/ROOT
unzip -q $WAR_FILE -d $PROJECT_DIR/ROOT
cp $PROJECT_DIR/dubbo.properties $DOC_ROOT/WEB-INF/classes/
find $PROJECT_DIR/ROOT -name "*.vm" |xargs sed -i 's/szone/'$STR_DATE'/g'

#restart tomcat
echo
echo Restart tomcat:
ps aux |grep -v grep |grep $TOMCAT|awk '{print $2}'|xargs kill -9
rm -rf $TOMCAT/work/Catalina
# sleep 10s
echo "Waiting shutdown Tomcat, Sleep 10s..."
sleep 10s
echo "Start to startup Tomcat..."
$TOMCAT/bin/startup.sh
